// const CryptoJS = require("crypto-js");
const storage = window.localStorage;
// const key = CryptoJS.enc.Utf8.parse('7e^V9FLMatcyX0kA').toString();
// const iv = CryptoJS.enc.Utf8.parse('kr6V%xV&tQj8kH13').toString();
export default {
    set (name, value) {
        if (typeof value === 'object') {
            value = JSON.stringify(value);
        };
        // Triple DES 加密
        // let encrypted = CryptoJS.TripleDES.encrypt(value.toString(), key, {
        //   iv: iv,
        //   mode: CryptoJS.mode.CBC,
        //   padding: CryptoJS.pad.Pkcs7
        // }).toString()
        // storage.setItem(name, encrypted)
        storage.setItem(name, value);
    },
    get (name) {
        let temp = storage.getItem(name);
        if (!temp) {
            return null;
        };
        // Triple DES 解密
        // let decrypted = CryptoJS.TripleDES.decrypt(temp, key, {
        //   iv: iv,
        //   mode: CryptoJS.mode.CBC,
        //   padding: CryptoJS.pad.Pkcs7
        // });
        // 转换为 utf8 字符串
        // decrypted = CryptoJS.enc.Utf8.stringify(decrypted) || temp;

        try {
            // return JSON.parse(decrypted);
            return JSON.parse(temp);
        } catch (e) {
            return temp;
        }
    },
    clear (name) {
        if (name) {
            storage.removeItem(name);
        } else {
            storage.clear();
        }
    },
    clearExcept (array) {
        if (!array) {
            this.clear();
        } else {
            let temp = [];
            for (let i = 0; i < array.length; i++) {
                let val = this.get(array[i]);
                if (val) {
                    temp.push({ name: array[i], val: val });
                };
            }
            this.clear();
            for (let i = 0; i < temp.length; i++) {
                this.set(temp[i].name, temp[i].val);
            };
        };
    }
};
